﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAL;
using Entity;
namespace CafeTek
{
    public partial class SplitTable : Form
    {
        public int Tableid;
        public IList<BillDetail> _listBillDetail;
        public Bill Gbill = new Bill();
        public BillDetail GbillDetailSource = null;
        public BillDetail GbillDetailDestination = null;
        public Bill GbillDestination = new Bill();
        public BillDetailDB _BillDetailDb = new BillDetailDB();
        public TableDB GTableDb = new TableDB();
        public int idTableDestination = -1;
        public Product GProductSource = null;
        public Product GproductDestination = null;
        public BillDB GBillDB = new BillDB();
        public int quanitySource = -1;
        public int Pricesource = -1;

        public SplitTable()
        {
            InitializeComponent();
        }
        
        /// <summary>
        /// Load all table Available to combobox
        /// </summary>
        public void LoadTableAvailable()
        {
            GTableDb.Connect();
            cmbAvailableTable.DataSource = GTableDb.getTableAvailable();

        }

        public void LoadProductTable()
        {
            _listBillDetail = new List<BillDetail>();
            LstvSourceTable.Items.Clear();
            int TableId = FrmMain.tableId;
            Table _table = new Table();
            _table.TableId = TableId;

            Gbill = _BillDetailDb.GetBillFromTableId(TableId);
            Gbill.Table = _table;
            _listBillDetail = _BillDetailDb.getAll(Gbill);
            int autonumber = 0;
            foreach (BillDetail item in _listBillDetail)
            {
                ListViewItem _itemlistview = new ListViewItem((++autonumber).ToString());
                _itemlistview.SubItems.Add(item.Product.Name);
                _itemlistview.Tag = item.Product.ProductId;
                _itemlistview.SubItems.Add(item.Quantity.ToString());
                _itemlistview.SubItems.Add(item.Price.ToString());
                LstvSourceTable.Items.Add(_itemlistview);
            }

        }

        private void SplitTable_Load(object sender, EventArgs e)
        {
            Tableid = FrmMain.tableId;
            this.Text = "Split Product from " + FrmMain.Gtable.Name;
            Gbill = _BillDetailDb.GetBillFromTableId(Tableid);
            LoadTableAvailable();
            LoadProductTable();

        }

        private void cmbAvailableTable_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (this.cmbAvailableTable.SelectedItem != null)
            {
                LstDestinationTable.Items.Clear();
                Table value = (Table)this.cmbAvailableTable.SelectedItem;
                idTableDestination = value.TableId;
                LoadProductTable();
            }
        }

        public void CheckTheBillInTable()
        {
            GbillDestination = _BillDetailDb.GetBillFromTableId(idTableDestination);
            if (GbillDestination == null)
            {
                frmLogin frm = new frmLogin();

                GbillDestination = GBillDB.AddNewBill(frmLogin._emp, idTableDestination);
                GbillDestination.TableExtent = Tableid;
                GBillDB.UpdateExtendTable(GbillDestination);
            }

        }

        public void AddProductToBill(BillDetail fbillDetail, Bill Fbill)
        {
            _BillDetailDb.AddProductToBIll(fbillDetail, Fbill);
        }

        private void btnmoverigh_Click(object sender, EventArgs e)
        {
            if (LstvSourceTable.SelectedItems.Count == 0)
                return;
            ListViewItem item = LstvSourceTable.SelectedItems[0];
            moveItemcount(LstvSourceTable, LstDestinationTable, LstvSourceTable.SelectedItems[0]);
        }

        public BillDetail InitBillDetailFromListview(ListViewItem item)
        {
            BillDetail _billdetail = new BillDetail();
            Product _product = new Product();
            _product.ProductId = (int)item.Tag;
            _product.Name = item.SubItems[1].Text;
            _billdetail.Product = _product;
            _billdetail.Quantity = int.Parse(item.SubItems[2].Text);
            _billdetail.Price = int.Parse(item.SubItems[3].Text);
            return _billdetail;
        }

        private void btnmoveallright_Click(object sender, EventArgs e)
        {
            if (LstvSourceTable.SelectedItems.Count == 0)
                return;
            ListViewItem item = LstvSourceTable.SelectedItems[0];
            moveItem(LstvSourceTable, LstDestinationTable, item);

        }

        public ListViewItem CheckItemInlistview(ListView flistview, ListViewItem fitemlistview)
        {
            for (int i = 0; i < flistview.Items.Count; i++)
            {
                if (flistview.Items[i].Tag == fitemlistview.Tag)
                    return flistview.Items[i];
            }
            return null;
        }

        public void InsertIntolistview(ListView flistview, ListViewItem flistviewitem)
        {
            int count = flistview.Items.Count;
            flistviewitem.Text = (count + 1).ToString();
            flistview.Items.Add(flistviewitem);
        }

        public ListViewItem CopyLIstviewItem(ListViewItem flistviewItem)
        {

            ListViewItem item = new ListViewItem(flistviewItem.Text);
            for (int i = 1; i <= 3; i++)
            {
                item.SubItems.Add(flistviewItem.SubItems[i].Text);
            }
            item.Tag = flistviewItem.Tag;
            return item;
        }

        public void moveItemcount(ListView flistsource, ListView flistdestination, ListViewItem fitemsource)
        {
            ListViewItem item = CopyLIstviewItem(fitemsource);

            ListViewItem itemcheck = CheckItemInlistview(flistdestination, item);
            if (itemcheck == null)
            {
                item.SubItems[2].Text = "1";
                InsertIntolistview(flistdestination, item);
            }
            else
                itemcheck.SubItems[2].Text = (int.Parse(itemcheck.SubItems[2].Text) + 1).ToString();
            if (fitemsource.SubItems[2].Text == "1")
                flistsource.Items.Remove(fitemsource);
            else
                fitemsource.SubItems[2].Text = (int.Parse(fitemsource.SubItems[2].Text) - 1).ToString();
        }

        public void moveItem(ListView flistsource, ListView flistdestination, ListViewItem fitemsource)
        {
            ListViewItem item = CopyLIstviewItem(fitemsource);
            flistsource.Items.Remove(fitemsource);
            ListViewItem itemcheck = CheckItemInlistview(LstDestinationTable, item);
            if (itemcheck == null)
                InsertIntolistview(flistdestination, item);
            else
                itemcheck.SubItems[2].Text = (int.Parse(itemcheck.SubItems[2].Text) + int.Parse(item.SubItems[2].Text)).ToString();
        }

        public void ReloadListview(ListView flistview)
        {
            int autonumber = 0;
            for (int i = 0; i < flistview.Items.Count; i++)
            {
                flistview.Items[i].Text = (++autonumber).ToString();
            }
        }

        private void BtnSave_Click(object sender, EventArgs e)
        {
            if (LstDestinationTable.Items.Count == 0)
            {
                MessageBox.Show("Choose one lest product to split");
                return;
            }
            _BillDetailDb.RemoveAllproductFromBill(Gbill);
            CheckTheBillInTable();
            for (int i = 0; i < LstDestinationTable.Items.Count; i++)
            {
                GbillDetailDestination = InitBillDetailFromListview(LstDestinationTable.Items[i]);
                AddProductToBill(GbillDetailDestination, GbillDestination);
            }
            for (int i = 0; i < LstvSourceTable.Items.Count; i++)
            {
                GbillDetailSource = InitBillDetailFromListview(LstvSourceTable.Items[i]);
                AddProductToBill(GbillDetailSource, Gbill);
            }
            LoadProductTable();
            LoadTableAvailable();
        }

        private void btnMoveLeft_Click(object sender, EventArgs e)
        {
            if (LstDestinationTable.SelectedItems.Count == 0)
                return;

            ListViewItem item = LstDestinationTable.SelectedItems[0];
            moveItemcount(LstDestinationTable, LstvSourceTable, LstDestinationTable.SelectedItems[0]);
        }

        private void btnmoveAllLeft_Click(object sender, EventArgs e)
        {
            if (LstDestinationTable.SelectedItems.Count == 0)
                return;
            ListViewItem item = LstDestinationTable.SelectedItems[0];
            moveItem(LstDestinationTable, LstvSourceTable, item);
        }
    }
}
